<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>学生管理</el-breadcrumb-item>
      <el-breadcrumb-item>未选志愿学生列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <!-- 渲染表格 -->
      <el-table :data="studentList" border stripe>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="学生id" prop="_id"></el-table-column>
        <el-table-column label="学生姓名" prop="username"></el-table-column>
        <el-table-column label="学生邮箱" prop="email"></el-table-column>
        <el-table-column label="学生志愿" prop="username">
          <el-tag type="danger">未选择</el-tag>
        </el-table-column>
        <el-table-column label="操作">
          <!-- 作用域插槽 -->
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-setting"
              size="mini"
              @click="showAddDialog(scope.row)"
            >添加志愿</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 添加志愿对话框 -->
      <el-dialog title="添加" :visible.sync="addDialogVisible" width="30%" @close="resetAddForm">
        <!-- 添加表单 -->
        <el-form
          :model="addForm"
          :rules="addRules"
          ref="addFormRef"
          label-width="100px"
          label-position="top"
        >
          <!-- 指导老师 -->
          <el-form-item label="指导老师" prop="teacher1">
            <!-- 选择器 -->
            <el-select v-model="addForm.teacher1" placeholder="请选择" @change="TselectChange">
              <el-option
                v-for="(item, index) in teacherList"
                :key="index"
                :label="item.username"
                :value="item._id"
              ></el-option>
            </el-select>
          </el-form-item>
          <!-- 题目 -->
          <el-form-item label="题目" prop="title1">
            <!-- 选择器 -->
            <el-select v-model="addForm.title1" placeholder="请选择">
              <el-option
                v-for="(item, index) in articleList"
                :key="index"
                :label="item.title"
                :value="item._id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="addDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addTitle">确 定</el-button>
        </span>
      </el-dialog>

      <!-- 分页功能 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[3, 5, 15, 30]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 查询参数
      queryInfo: {
        query: 'notselect',
        pagenum: 1,
        pagesize: 15
      },
      // 未选志愿学生列表
      studentList: [],
      total: 0,
      // 题目列表
      articleList: [],
      // 指导老师列表
      teacherList: [],
      // 控制对话框显示与否
      addDialogVisible: false,
      // 添加题目表单
      addForm: {
        teacher1: '',
        title1: ''
      },
      // 表单验证规则
      addRules: {
        teacher1: [{ required: true, message: '请选择指导老师', trigger: 'blur' }],
        title1: [{ required: true, message: '请选择题目', trigger: 'blur' }]
      },
      // 点击添加按钮时,保存对应学生id
      id: ''
    }
  },
  created() {
    this.getStudentList()
  },
  methods: {
    async getStudentList() {
      // 发送请求，获取未选志愿学生列表
      const { data: res } = await this.$http.get('users', {
        params: this.queryInfo
      })
      // 请求成功后赋值
      this.studentList = res.records
      this.total = res.total
      console.log(this.studentList)
    },
    // 获取题目列表
    async getArticleList() {
      const { data: res } = await this.$http.get('articles')
      // 判断请求结果
      if (res.meta.status !== 200) {
        return this.$message.error('获取题目列表失败')
      }
      // 请求成功
      // 过滤出Tauthor._id的值等于this.addForm.teacher1的数据
      // 还有过滤得出对应指导老师的“已被审核”题目
      let result = res.data.records
      result = result.filter(item => {
        return (item.Tauthor._id === this.addForm.teacher1) && (item.checked === true)
      })
      this.articleList = result
      console.log(this.articleList)
    },
    // 获取指导老师列表
    async getTeacherList() {
      // 发送请求，获取指导老师列表
      const { data: res } = await this.$http.get('users', {
        params: { query: 'teacher' }
      })
      // 请求成功后赋值
      this.teacherList = res.records
      console.log(this.teacherList)
    },
    // 每页显示数据条数改变
    handleSizeChange(newsize) {
      this.queryInfo.pagesize = newsize
      this.getStudentList()
    },
    // 当前页码改变
    handleCurrentChange(newpage) {
      this.queryInfo.pagenum = newpage
      this.getStudentList()
    },
    // 显示对话框
    showAddDialog(row) {
      this.getTeacherList()
      this.addDialogVisible = true
      // 保存学生id
      this.id = row._id
    },
    // 指导老师选择器change事件
    TselectChange() {
      // 获取题目列表
      this.getArticleList()
      console.log(this.addForm.titles)
    },
    // 确定提交志愿
    addTitle() {
      // 预验证
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) {
          return this.$message.error('验证未通过')
        }
        // 验证通过
        console.log(this.addForm)
        // 发送请求.添加到数据库
        const { data: res } = await this.$http.post(
          'students/select/' + this.id,
          {
            title1: this.addForm.title1
          }
        )
        // 判断请求结果
        if (res.meta.status !== 200) {
          return this.$message.error('添加失败')
        }
        // 请求成功,给出提示
        this.$message.success('添加成功')
        // 重新获取列表
        this.getStudentList()
        this.addDialogVisible = false
      })
    },
    // 对话框关闭事件
    resetAddForm() {
      this.$refs.addFormRef.resetFields()
    }
  }
}
</script>
